<template>
  <div class="container">
    <el-card class="content-card">
      <el-form ref="searchFormRef" :model="searchData">
        <el-form-item prop="dbname" label="数据库名称：">
          <el-select v-model="searchData.dbname" placeholder="请选择" class="search-ipt">
            <el-option v-for="item in list" :key="item.id" :label="item.dbtitle" :value="item.dbname" />
          </el-select>
          <el-button class="left-btn" type="primary" :icon="Search" @click="handleSearch">查询</el-button>
          <el-button :icon="Refresh" @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
      <div class="table-wrapper">
        <el-table
          :data="tableData"
          border
          style="width: 100%"
          :row-class-name="rowClassName"
          max-height="calc(100vh - 270px)"
        >
          <el-table-column label="序号" width="60">
            <template #default="scope">
              {{ scope.row.index + 1 }}
            </template>
          </el-table-column>
          <el-table-column prop="dbtitle" label="数据库名称" width="130" />
          <el-table-column prop="question" label="问题" width="250" />
          <el-table-column prop="answer" label="答案">
            <template #default="scope">
              <ClampText :text="scope.row.answer" />
            </template>
          </el-table-column>
          <el-table-column label="操作" width="100">
            <template #default="scope">
              <el-tooltip class="box-item" effect="dark" content="通过" placement="top">
                <el-icon color="green" size="16" @click="audit(scope.row)" class="icon-left"><CircleCheck /></el-icon>
              </el-tooltip>
              <el-tooltip class="box-item" effect="dark" content="拒绝" placement="top">
                <el-icon color="#E34D59" size="16" @click="refuse(scope.row)"><CircleClose /></el-icon>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pager-wrapper" v-if="paginationData.total > 0">
        <div class="totalCount">共{{ paginationData.total }}项数据</div>
        <el-pagination
          background
          :layout="paginationData.layout"
          :total="paginationData.total"
          :page-size="paginationData.pageSize"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue"
import { debounce } from "lodash-es"
import { ElMessage, ElLoading } from "element-plus"
import { reviewList, selDel, selAudit } from "@/api/konwledge"
import { getDbNameList } from "@/api/sql"
import ClampText from "@/components/ClampText/index.vue"

onMounted(() => {
  getTableData()
  getlist()
})
const searchFormRef = ref(null)
const searchData = ref({
  dbname: ""
})
const handleSearch = () => {
  paginationData.value.currentPage
  getTableData()
}
const resetSearch = () => {
  searchFormRef.value?.resetFields()
  handleSearch()
}

const list = ref([])
async function getlist() {
  const { code, data } = await getDbNameList()
  if (code === 200) {
    list.value = data
  }
}
const paginationData = ref({
  total: 0,
  currentPage: 1,
  pageSize: 10,
  layout: "prev, pager, next"
})
/** 改变当前页码 */
function handleCurrentChange(value) {
  if (paginationData.value.currentPage === value) return
  paginationData.value.currentPage = value
  getTableData()
}

const tableData = ref([])

const refuse = debounce(async (row) => {
  const { code } = await selDel({ qid: row.qid })
  if (code === 200) {
    ElMessage.success("已删除")
    getTableData()
  }
}, 100)
// debounce
const audit = debounce(async (row) => {
  const { code } = await selAudit({ qid: row.qid })
  if (code === 200) {
    ElMessage.success("成功加入知识库")
    getTableData()
  }
}, 100)
function rowClassName({ row, rowIndex }) {
  row.index = rowIndex
}

async function getTableData() {
  const wrap = document.getElementsByClassName("table-wrapper")[0]
  const loadingInstance = ElLoading.service({
    target: wrap,
    text: "Loading"
  })
  const { code, data } = await reviewList({
    dbname: searchData.value.dbname,
    page: paginationData.value.currentPage,
    pageSize: paginationData.value.pageSize
  })
  if (code === 200) {
    tableData.value = data.resultList
    paginationData.value.total = data.totalCount
  }
  loadingInstance.close()
}
</script>
<style lang="scss" scoped>
/* 设置表头背景色 */
::v-deep(.el-table__header-wrapper th) {
  background-color: #f5f5f5; /* 背景色 */
  color: #333; /* 文字颜色 */
  font-weight: bold; /* 加粗字体 */
}
:deep(.el-form-item) {
  margin-bottom: 0 !important;
}
.icon-left {
  margin-right: 20px;
}
.left-btn {
  margin-left: 10px;
}
.search-ipt {
  width: 250px;
}
.content-card {
  margin-top: 20px;
}
.add-wrap {
  padding: 20px;
  display: flex;
}
.table-wrapper {
  padding: 20px 0;
}
.pager-wrapper {
  color: #666;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
</style>
